package com.jl.jlapp.mvp.fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter.OnItemChildClickListener;
import com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener;
import com.chad.library.adapter.base.BaseQuickAdapter.RequestLoadMoreListener;
import com.jl.jlapp.R;
import com.jl.jlapp.adapter.UserUnUsableCouponAdapter;
import com.jl.jlapp.adapter.UserUsableCouponAdapter;
import com.jl.jlapp.eneity.UserCouponInfosModel;
import com.jl.jlapp.eneity.UserCouponInfosModel.ResultDataBean.DisabledCouponBean;
import com.jl.jlapp.eneity.UserCouponInfosModel.ResultDataBean.UsableCouponBean;
import com.jl.jlapp.mvp.activity.CouponHasGoodsListActivity;
import com.jl.jlapp.mvp.activity.LoginActivity;
import com.jl.jlapp.nets.Api;
import com.jl.jlapp.nets.CommonObserver;
import com.jl.jlapp.nets.Net;
import com.jl.jlapp.nets.Transformer;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import java.util.ArrayList;
import java.util.List;

public class UserCouponFragment extends LazyLoadFragment implements RequestLoadMoreListener {
    private static final String EXTRA_TYPE = "extra_type";
    private static final int PAGE_SIZE = 10;
    public static final int TYPE_UNUSEABLE = 2;
    public static final int TYPE_USEABLE = 1;
    private int TOTAL_COUNTER_UNABLED = 0;
    private int TOTAL_COUNTER_USABLED = 0;
    private int delayMillis = 1000;
    @BindView(R.id.lrv_discount_list)
    RecyclerView lrvDiscountList;
    private int mCurrentCounter = 0;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.no_coupon_show)
    RelativeLayout noCouponShow;
    public OnGetCouponMsg onGetCouponMsg;
    private ProgressDialog progressDialog;
    public setOnClickCouponItem setOnClickCouponItem;
    List<UserCouponInfosModel.ResultDataBean.DisabledCouponBean> shouldShowUnUsableCouponsBeanList = new ArrayList();
    List<UserCouponInfosModel.ResultDataBean.UsableCouponBean> shouldShowUsableCouponsBeanList = new ArrayList();
    int type = 1;
    List<UserCouponInfosModel.ResultDataBean.DisabledCouponBean> unUsableCouponsBeanList = new ArrayList();
    Unbinder unbinder;
    List<UserCouponInfosModel.ResultDataBean.UsableCouponBean> usableCouponsBeanList = new ArrayList();
    int userId = 0;
    private UserUnUsableCouponAdapter userUnUsableCouponAdapter;
    private UserUsableCouponAdapter userUsableCouponAdapter;

    private void initDatas(int paramInt) {
        switch (paramInt) {
            default:
                return;
            case 1:
                if (this.onGetCouponMsg != null) {
                    this.onGetCouponMsg.onGetCouponMsg(this.TOTAL_COUNTER_USABLED, this.TOTAL_COUNTER_UNABLED);
                }
                if (this.shouldShowUsableCouponsBeanList.size() > 0) {
                    this.lrvDiscountList.setVisibility(View.VISIBLE);
                    this.noCouponShow.setVisibility(View.GONE);
                    if (this.userUsableCouponAdapter == null) {
                        Log.d("aaaaaaaaaaaaa", "新建 shouldShowUsableCouponsBeanList：" + this.shouldShowUsableCouponsBeanList.size());
                        this.lrvDiscountList.setLayoutManager(new LinearLayoutManager(getActivity()));
                        this.userUsableCouponAdapter = new UserUsableCouponAdapter(R.layout.item_coupon, this.shouldShowUsableCouponsBeanList);
                        this.userUsableCouponAdapter.setOnLoadMoreListener(this, this.lrvDiscountList);
                        this.lrvDiscountList.setAdapter(this.userUsableCouponAdapter);
                        this.mCurrentCounter = this.userUsableCouponAdapter.getData().size();
                        this.userUsableCouponAdapter.setOnItemClickListener(new OnItemClickListener() {
                            public void onItemClick(BaseQuickAdapter paramAnonymousBaseQuickAdapter, View paramAnonymousView, int paramAnonymousInt) {
                                TextView tv_discount_money = paramAnonymousView.findViewById(R.id.tv_discount_money);
                                TextView tv_discount_condition = (TextView) paramAnonymousView.findViewById(R.id.tv_discount_condition);
                                paramAnonymousInt = ((UserCouponInfosModel.ResultDataBean.UsableCouponBean) UserCouponFragment.this.shouldShowUsableCouponsBeanList.get(paramAnonymousInt)).getId();
                                double d = Double.parseDouble(tv_discount_money.getText().toString().trim());
                                String trim = tv_discount_condition.getText().toString().trim();
                                if (UserCouponFragment.this.setOnClickCouponItem != null) {
                                    UserCouponFragment.this.setOnClickCouponItem.onClickCouponItem(paramAnonymousInt, d, trim);
                                }
                            }
                        });
                        this.userUsableCouponAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                            public void onItemChildClick(BaseQuickAdapter paramAnonymousBaseQuickAdapter, View paramAnonymousView, int paramAnonymousInt) {
                                Intent intent = new Intent(UserCouponFragment.this.getContext(), CouponHasGoodsListActivity.class);
                                intent.putExtra("couponId", ((UserCouponInfosModel.ResultDataBean.UsableCouponBean) UserCouponFragment.this.shouldShowUsableCouponsBeanList.get(paramAnonymousInt)).getCouponInformationId());
                                UserCouponFragment.this.startActivity(intent);
                            }
                        });
                        return;
                    }
                    Log.d("aaaaaaaaaaaaa", "刷新 shouldShowUsableCouponsBeanList：" + this.shouldShowUsableCouponsBeanList.size());
                    this.userUsableCouponAdapter.notifyDataSetChanged();
                    this.mCurrentCounter = this.userUsableCouponAdapter.getData().size();
                    return;
                }
                this.lrvDiscountList.setVisibility(View.GONE);
                this.noCouponShow.setVisibility(View.VISIBLE);
        }
        if (this.shouldShowUnUsableCouponsBeanList.size() > 0) {
            this.lrvDiscountList.setVisibility(View.VISIBLE);
            this.noCouponShow.setVisibility(View.GONE);
            if (this.userUnUsableCouponAdapter == null) {
                Log.d("aaaaaaaaaaaaa", "新建 shouldShowUnUsableCouponsBeanList：" + this.shouldShowUnUsableCouponsBeanList.size());
                this.lrvDiscountList.setLayoutManager(new LinearLayoutManager(getActivity()));
                this.userUnUsableCouponAdapter = new UserUnUsableCouponAdapter(R.layout.item_coupon, this.shouldShowUnUsableCouponsBeanList);
                this.userUnUsableCouponAdapter.setOnLoadMoreListener(this, this.lrvDiscountList);
                this.lrvDiscountList.setAdapter(this.userUnUsableCouponAdapter);
            } else {
                this.mCurrentCounter = this.userUnUsableCouponAdapter.getData().size();
                this.userUnUsableCouponAdapter.notifyDataSetChanged();
            }
        }
        this.lrvDiscountList.setVisibility(View.GONE);
        this.noCouponShow.setVisibility(View.VISIBLE);
    }

    public static UserCouponFragment newInstance(int paramInt1, int paramInt2) {
        UserCouponFragment localUserCouponFragment = new UserCouponFragment();
        Bundle localBundle = new Bundle();
        localBundle.putInt("extra_type", paramInt1);
        localBundle.putInt("userId", paramInt2);
        localUserCouponFragment.setArguments(localBundle);
        return localUserCouponFragment;
    }

    private void setShouldShowUnUsableCouponsBeanList(int paramInt1, int paramInt2) {
        this.shouldShowUnUsableCouponsBeanList.clear();
        if ((this.unUsableCouponsBeanList.size() > paramInt1) && (this.unUsableCouponsBeanList.size() > paramInt1 + paramInt2)) {
            for (int i = 0; i < paramInt1 + paramInt2; i++) {
                this.shouldShowUnUsableCouponsBeanList.add(this.unUsableCouponsBeanList.get(i));
            }
        } else if ((this.unUsableCouponsBeanList.size() > paramInt1) && (this.unUsableCouponsBeanList.size() <= paramInt1 + paramInt2)) {
            for (int i = 0; i < unUsableCouponsBeanList.size(); i++) {
                this.shouldShowUnUsableCouponsBeanList.add(this.unUsableCouponsBeanList.get(paramInt1));
            }
        }
        this.TOTAL_COUNTER_UNABLED = this.unUsableCouponsBeanList.size();
    }

    private List<UserCouponInfosModel.ResultDataBean.DisabledCouponBean> setShouldShowUnUsableCouponsBeanList2(int paramInt1, int paramInt2) {
        ArrayList<UserCouponInfosModel.ResultDataBean.DisabledCouponBean> localArrayList = new ArrayList();
        if ((this.unUsableCouponsBeanList.size() > paramInt1) && (this.unUsableCouponsBeanList.size() > paramInt1 + paramInt2)) {
            for (int i = 0; i < paramInt1 + paramInt2; i++) {
                localArrayList.add(this.unUsableCouponsBeanList.get(i));
            }
        } else if ((this.unUsableCouponsBeanList.size() > paramInt1) && (this.unUsableCouponsBeanList.size() <= paramInt1 + paramInt2)) {
            for (int i = 0; i < unUsableCouponsBeanList.size(); i++) {
                localArrayList.add(this.unUsableCouponsBeanList.get(i));
            }
        }
        return localArrayList;
    }

    private void setShouldShowUsableCouponsBeanList(int paramInt1, int paramInt2) {
        this.shouldShowUsableCouponsBeanList.clear();
        if ((this.usableCouponsBeanList.size() > paramInt1) && (this.usableCouponsBeanList.size() > paramInt1 + paramInt2)) {
            for (int i = 0; i < paramInt1 + paramInt2; i++) {
                this.shouldShowUsableCouponsBeanList.add(this.usableCouponsBeanList.get(i));
            }
        } else if ((this.usableCouponsBeanList.size() > paramInt1) && (this.usableCouponsBeanList.size() <= paramInt1 + paramInt2)) {
            for (int i = 0; i < usableCouponsBeanList.size(); i++)
                this.shouldShowUsableCouponsBeanList.add(this.usableCouponsBeanList.get(paramInt1));
        }
        this.TOTAL_COUNTER_USABLED = this.usableCouponsBeanList.size();
    }

    private List<UserCouponInfosModel.ResultDataBean.UsableCouponBean> setShouldShowUsableCouponsBeanList2(int paramInt1, int paramInt2) {
        ArrayList<UserCouponInfosModel.ResultDataBean.UsableCouponBean> localArrayList = new ArrayList();
        if ((this.usableCouponsBeanList.size() > paramInt1) && (this.usableCouponsBeanList.size() > paramInt1 + paramInt2)) {
            for (int i = 0; i < paramInt1 + paramInt2; i++) {
                localArrayList.add(this.usableCouponsBeanList.get(i));
            }
        } else if ((this.usableCouponsBeanList.size() > paramInt1) && (this.usableCouponsBeanList.size() <= paramInt1 + paramInt2)) {
            for (int i = 0; i < this.usableCouponsBeanList.size(); i++) {
                localArrayList.add(this.usableCouponsBeanList.get(paramInt1));
            }
        }
        return localArrayList;
    }

    public void buildProgressDialog() {
        if (this.progressDialog == null) {
            this.progressDialog = new ProgressDialog(getContext());
            this.progressDialog.setProgressStyle(0);
        }
        this.progressDialog.setMessage("清除中");
        this.progressDialog.setCancelable(true);
        this.progressDialog.show();
    }

    public void cancelProgressDialog() {
        if ((this.progressDialog != null) && (this.progressDialog.isShowing())) {
            this.progressDialog.dismiss();
        }
    }

    public void getCouponInfoByUserId(int paramInt) {
        Net.get().getCouponInfoByUserId(paramInt)
                .compose(Transformer.<UserCouponInfosModel>switchSchedulers())
                .subscribe(new CommonObserver<UserCouponInfosModel>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(UserCouponInfosModel userCouponInfosModel) {

                    }
                });
    }

    public int initLayout() {
        return R.layout.fragment_discount;
    }

    public void initViews(View paramView) {
        Bundle localBundle = getArguments();
        if ((localBundle == null) || (!localBundle.containsKey("extra_type"))) {
            throw new IllegalArgumentException("NoticeListFragment must be created by type args");
        }
        this.type = localBundle.getInt("extra_type", 1);
        this.userId = localBundle.getInt("userId", 0);
        this.unbinder = ButterKnife.bind(this, paramView);
        this.mSwipeRefreshLayout.setEnabled(false);
    }

    public void loadData() {
        if (this.userId > 0) {
            buildProgressDialog();
            getCouponInfoByUserId(this.userId);
            return;
        }
        Toast.makeText(getContext(), "您还未登录，请先登录。", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(getContext(), LoginActivity.class));
    }

    public void onLoadMoreRequested() {
        this.mSwipeRefreshLayout.setEnabled(false);
        switch (this.type) {
            default:
                return;
            case 1:
                if (this.userUsableCouponAdapter.getData().size() < 10) {
                    this.userUsableCouponAdapter.loadMoreEnd(false);
                    return;
                }
                if (this.mCurrentCounter >= this.TOTAL_COUNTER_USABLED) {
                    this.userUsableCouponAdapter.loadMoreEnd(false);
                    return;
                }
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        new ArrayList();
                        List localList = UserCouponFragment.this.setShouldShowUsableCouponsBeanList2(UserCouponFragment.this.userUsableCouponAdapter.getData().size(), 10);
                        UserCouponFragment.this.userUsableCouponAdapter.addData(localList);
                        UserCouponFragment.this.userUsableCouponAdapter.loadMoreComplete();
                        UserCouponFragment.this.mSwipeRefreshLayout.setEnabled(true);
                    }
                }, this.delayMillis);
        }
        if (this.userUnUsableCouponAdapter.getData().size() < 10) {
            this.userUnUsableCouponAdapter.loadMoreEnd(false);
            return;
        }
        if (this.mCurrentCounter >= this.TOTAL_COUNTER_UNABLED) {
            this.userUnUsableCouponAdapter.loadMoreEnd(false);
            return;
        }
        new Handler().postDelayed(new Runnable() {
            public void run() {
                UserCouponFragment.this.userUnUsableCouponAdapter.addData(UserCouponFragment.this.setShouldShowUnUsableCouponsBeanList2(UserCouponFragment.this.userUnUsableCouponAdapter.getData().size(), 10));
                UserCouponFragment.this.userUnUsableCouponAdapter.loadMoreComplete();
                UserCouponFragment.this.mSwipeRefreshLayout.setEnabled(true);
            }
        }, this.delayMillis);
    }

    public void setOnGetCouponMsg(OnGetCouponMsg paramOnGetCouponMsg) {
        this.onGetCouponMsg = paramOnGetCouponMsg;
    }

    public void setSetOnClickCouponItem(setOnClickCouponItem paramsetOnClickCouponItem) {
        this.setOnClickCouponItem = paramsetOnClickCouponItem;
    }

    public static abstract interface OnGetCouponMsg {
        public abstract void onGetCouponMsg(int paramInt1, int paramInt2);
    }

    public static abstract interface setOnClickCouponItem {
        public abstract void onClickCouponItem(int paramInt, double paramDouble, String paramString);
    }
}


/* Location:              /Users/davidzhang/Documents/decompileApk/jlfood-dex2jar.jar!/com/jl/jlapp/mvp/fragment/UserCouponFragment.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */